package com.fare.taluo.core.domain.queue.message;

import javax.persistence.Column;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Where;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.core.domain.queue.QueueBaseEntity;
import com.fare.taluo.core.domain.queue.QueueBaseRepository;
import com.fare.taluo.core.domain.queue.message.repository.impl.QueueSMSMessageRepositoryImpl;
import com.fare.taluo.message.SendSMS;
@javax.persistence.Entity
@Table(name="que_sms_message")
@Where(clause="flg_deleted=0")
public class QueueSMSMessageEntity extends QueueBaseEntity<QueueSMSMessageEntity>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4317305142344198694L;
	/**
	 * 发送目标手机号
	 */
	@Column(name = "TO_PHONE")
	private String toPhone;
	/**
	 * 内容
	 */
	@Column(name = "CONTENT")
	private String content;
	
	public String getToPhone() {
		return toPhone;
	}
	public void setToPhone(String toPhone) {
		this.toPhone = toPhone;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	@Override
	protected QueueBaseRepository<QueueSMSMessageEntity> getEntityRepository() {
		return InstanceFactory.getInstance(QueueSMSMessageRepositoryImpl.class);
	}
	@Override
	public QueueSMSMessageEntity createNextTask(Long id) throws Exception {
		if(logger.isDebugEnabled()){
			logger.debug("短信队列，不需要周期性创建下一个任务");
		}
		return null;
	}
	@Override
	public void executeTask(Long id) throws Exception {
		QueueSMSMessageEntity entity = getEntityRepository().get(this.getClass(), id);
		if(entity != null){
			//短信没有多个帐号设置发送方法，这里要拆分成多次请求发送
			if(StringUtils.isNotEmpty(entity.getToPhone())){
				String[] phones = entity.getToPhone().split(";");
				if(phones != null && phones.length > 0){
					for(String p : phones){
						if(StringUtils.isNotEmpty(p))
							new SendSMS().setAccount(p).setContent(entity.getContent()).send();
					}
				}
			}
		}
	}
}
